#ifndef __BUOLA_GL_CFRAMEBUFFER_H__
#define __BUOLA_GL_CFRAMEBUFFER_H__

#include <buola/gl.h>
#include <buola/gl/crenderbuffer.h>

namespace buola { namespace gl {

class CFrameBuffer
{
public:
    enum EType
    {
        DRAW=GL_DRAW_FRAMEBUFFER,
        READ=GL_READ_FRAMEBUFFER
    };

    CFrameBuffer()
        :   mID(0)
    {}

    ~CFrameBuffer()
    {
        if(mID)
            Delete();
    }

    void Create()
    {
        if(mID) Delete();
        glGenFramebuffers(1,&mID);
    }

    void Bind(EType pType)
    {
        glBindFramebuffer(pType,mID);
    }

    static void Unbind(EType pType)
    {
        glBindFramebuffer(pType,0);
    }

    void Delete()
    {
        glDeleteFramebuffers(1,&mID);
        mID=0;
    }

    GLuint GetID() const     {   return mID; }

    void AttachDepth(EType pType,const CRenderBuffer &pBuffer)
    {
        glFramebufferRenderbuffer(pType,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,pBuffer.GetID());
    }

    void AttachColor(EType pType,const CRenderBuffer &pBuffer,int pNumber=0)
    {
        glFramebufferRenderbuffer(pType,GL_COLOR_ATTACHMENT0+pNumber,GL_RENDERBUFFER,pBuffer.GetID());
    }

    void AttachStencil(EType pType,const CRenderBuffer &pBuffer)
    {
        glFramebufferRenderbuffer(pType,GL_STENCIL_ATTACHMENT,GL_RENDERBUFFER,pBuffer.GetID());
    }

    void AttachDepthStencil(EType pType,const CRenderBuffer &pBuffer)
    {
        glFramebufferRenderbuffer(pType,GL_DEPTH_STENCIL_ATTACHMENT,GL_RENDERBUFFER,pBuffer.GetID());
    }

private:
    GLuint mID;
};

/*namespace gl*/ } /*namespace buola*/ }

#endif
